var matchStatsTimeStamp = 1;
var GameID = $('input[name="gameID"]').val();
var xhrMatchStats;
var isxhrMatchStatsActive = false;



function getMatchStats()
{
    location.hash = "livefeed";
 //isxhrMatchStatsActive = true;
    var serverURL = '/dazzler/longpoller/';
     if (mainStatsObj.Innings == null)
        {
           currentInnings=1;
        }
        else
        {
           currentInnings = mainStatsObj.Innings;
        }
 var dataToPost= {  'pollType':'mymatchStats','GameID': GameID ,'matchStatsTimeStamp':matchStatsTimeStamp,'innings':currentInnings};

  xhrMatchStats = $.ajax({
       type: 'post',
       url: serverURL,
       data: dataToPost,
       dataType: "json",
       async: true,
       cache: false ,
       beforesend: function() {


  },
       success: function(data)
       {
       // isxhrMatchStatsActive = true;
        setUpFallofWicketsData(data.fallofWicketsInningsOne,data.fallofWicketsInningsTwo);
        SetUpManhattanData(data.manhattanInningsOne,data.manhattanInningsTwo);
        setUpRunsTypeData(data.runsTypeInningsOne,data.runsTypeInningsTwo);

      //  populateMainStats(data.score,data.ballCommentary,data.teams);
         matchStatsTimeStamp = data.newMatchStatsTimeStamp;
       //  waitForMatchStats();


       },
            error: function(XMLHttpRequest, textStatus, errorThrown){

                $("#pageheader").html("Match Stats Long Poller has been switched off.");
                isxhrMatchStatsActive = false;
//                setTimeout(
//
//               'waitForScoreCardStats()', /* Try again after.. */
//                    "5000"); /* milliseconds (5 seconds) */
            }

    });
}

function waitForMatchStats()
{
  location.hash = "livefeed";
 isxhrMatchStatsActive = true;
    var serverURL = '/dazzler/longpoller/';
     if (mainStatsObj.Innings == null)
        {
           currentInnings=1;
        }
        else
        {
           currentInnings = mainStatsObj.Innings;
        }
 var dataToPost= {  'pollType':'mymatchStats','GameID': GameID ,'matchStatsTimeStamp':matchStatsTimeStamp,'innings':currentInnings};

  xhrMatchStats = $.ajax({
       type: 'post',
       url: serverURL,
       data: dataToPost,
       dataType: "json",
       async: true,
       cache: false ,
       beforesend: function() {

   
  },
       success: function(data)
       {
        isxhrMatchStatsActive = true;
        setUpFallofWicketsData(data.fallofWicketsInningsOne,data.fallofWicketsInningsTwo);
        SetUpManhattanData(data.manhattanInningsOne,data.manhattanInningsTwo);
        setUpRunsTypeData(data.runsTypeInningsOne,data.runsTypeInningsTwo);
        
      //  populateMainStats(data.score,data.ballCommentary,data.teams);
         matchStatsTimeStamp = data.newMatchStatsTimeStamp;
         waitForMatchStats();


       },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                
                $("#pageheader").html("Match Stats Long Poller has been switched off.");
                isxhrMatchStatsActive = false;
//                setTimeout(
//
//               'waitForScoreCardStats()', /* Try again after.. */
//                    "5000"); /* milliseconds (5 seconds) */
            }

    });

}


var manhattanFirstInningOvers =[];
var manhattanFirstInningRuns = [];
var manhattanFirstInningWickets = [];

var manhattanSecondInningOvers =[];
var manhattanSecondInningRuns = [];
var manhattanSecondInningWickets = [];
function SetUpManhattanData(manhattanFirstInn,manhattanSecondInn)
{
                manhattanFirstInningOvers =[];
                manhattanFirstInningRuns = [];
                manhattanFirstInningWickets = [];
                
                $.each(manhattanFirstInn, function(increment, manhattanValue)
                    {
                        manhattanFirstInningRuns.push(parseInt(manhattanValue.runs));
                        manhattanFirstInningOvers.push(parseInt(manhattanValue.over));
                        manhattanFirstInningWickets.push(parseInt(manhattanValue.wickets));
                    });
                manhattanSecondInningOvers =[];
                manhattanSecondInningRuns = [];
                manhattanSecondInningWickets = [];
                
                                $.each(manhattanSecondInn, function(increment, manhattanValue)
                    {
                        manhattanSecondInningRuns.push(parseInt(manhattanValue.runs));
                        manhattanSecondInningOvers.push(parseInt(manhattanValue.over));
                        manhattanSecondInningWickets.push(parseInt(manhattanValue.wickets));
                    });
                    setUpMahattan();
}

var runsTypeFirstInnArray = [];
var runsTypeSecondInnArray = [];
function setUpRunsTypeData(runsTypeFirstInn,runsTypeSecondInn)
{
runsTypeFirstInnArray= [];
 runsTypeFirstInnArray.push(parseInt(runsTypeFirstInn.ones));
  runsTypeFirstInnArray.push(parseInt(runsTypeFirstInn.twos));
  runsTypeFirstInnArray.push(parseInt(runsTypeFirstInn.threes));
  runsTypeFirstInnArray.push(parseInt(runsTypeFirstInn.fours));
  runsTypeFirstInnArray.push(parseInt(runsTypeFirstInn.fives));
  runsTypeFirstInnArray.push(parseInt(runsTypeFirstInn.sixes));
runsTypeSecondInnArray=[];
  runsTypeSecondInnArray.push(parseInt(runsTypeSecondInn.ones));
  runsTypeSecondInnArray.push(parseInt(runsTypeSecondInn.twos));
  runsTypeSecondInnArray.push(parseInt(runsTypeSecondInn.threes));
  runsTypeSecondInnArray.push(parseInt(runsTypeSecondInn.fours));
  runsTypeSecondInnArray.push(parseInt(runsTypeSecondInn.fives));
  runsTypeSecondInnArray.push(parseInt(runsTypeSecondInn.sixes));
  setUpRunTypeChart();
}


var fallOfWicketsFirstInningsRuns = [];
var fallOfWicketsFirstInningsBatsman = [];
var fallOfWicketsFirstInningsOvers = [];


var fallOfWicketsSecondInningsRuns = [];
var fallOfWicketsSecondInningsBatsman = [];
var fallOfWicketsSecondInningsOvers = [];
function setUpFallofWicketsData(fallOfWicketsFirstInn,fallOfWicketsSecondInn)
{
     fallOfWicketsFirstInningsRuns = [];
     fallOfWicketsFirstInningsBatsman = [];
     fallOfWicketsFirstInningsOvers = [];
                     $.each(fallOfWicketsFirstInn, function(increment, fallOfWicketsValue)
                    {
                      
                       fallOfWicketsFirstInningsRuns.push(parseInt(fallOfWicketsValue.runs));
                        fallOfWicketsFirstInningsBatsman.push(fallOfWicketsValue.first_name+" "+fallOfWicketsValue.last_name);
                        fallOfWicketsFirstInningsOvers.push(parseFloat(fallOfWicketsValue.over));
                    });
     fallOfWicketsSecondInningsRuns = [];
     fallOfWicketsSecondInningsBatsman = [];
     fallOfWicketsSecondInningsOvers = [];
                          $.each(fallOfWicketsSecondInn, function(increment, fallOfWicketsValue)
                    {
                      
                       fallOfWicketsSecondInningsRuns.push(parseInt(fallOfWicketsValue.runs));
                        fallOfWicketsSecondInningsBatsman.push(fallOfWicketsValue.first_name+" "+fallOfWicketsValue.last_name);
                        fallOfWicketsSecondInningsOvers.push(parseFloat(fallOfWicketsValue.over));
                    });

                    setUpWorm();
}




